<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" fullscreen center>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="加工中心部门" prop="workDeptId">
        <el-select v-model="dataForm.workDeptId" placeholder="请选择">
          <el-option v-for="item in workDeptOption" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关联类型" prop="linkType">
        <el-radio-group v-model="dataForm.linkType" @change="changeType" :disabled="disFlag">
          <el-radio :label="1">部门</el-radio>
          <el-radio :label="2">单位</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-row>
        <el-col :span="21">
          <el-form-item label="查询部门" v-if="dataForm.linkType == 1">
            <el-input type="textarea" v-model="dataForm.linkName" disabled></el-input>
          </el-form-item>
          <el-form-item label="查询单位" v-if="dataForm.linkType == 2">
            <el-input type="textarea" v-model="dataForm.linkName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label-width="0">
            <el-button @click="openDeptDialog">选择</el-button>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <!-- 弹窗 部门列表 -->
    <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData"></search-dept>
    <!-- 弹窗 单位列表 -->
    <search-unit v-if="unitVisible" ref="unitDialog" @refreshData="getDeptData"></search-unit>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/eims/worklink'
  import { getDeptInfoByDeptTypes } from '@/api/admin/dept'

  import SearchDept from './SearchDept'
  import SearchUnit from '../his/SearchUnit'
  export default {
    data() {
      return {
        visible: false,
        deptVisible: false,
        unitVisible: false,
        disFlag: false,
        type: '',
        dataForm: {
          id: 0,
          workDeptId: '',
          linkId: '',
          linkType: 1,
          linkName: '',
          linkIds: []
        },
        workDeptOption: [],
        dataRule: {
          workDeptId: [{
            required: true,
            message: '加工中心部门不能为空',
            trigger: 'blur'
          }],
          linkId: [{
            required: true,
            message: '销售部门/单位不能为空',
            trigger: 'blur'
          }],
          linkType: [{
            required: true,
            message: '关联类型(1-部门,2-单位)不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    created() {
      this.getWorkDeptList()
    },
    components: {
      SearchDept,
      SearchUnit
    },
    methods: {
      init(data) {
        this.type = data.type
        if (data.type == 'edit') {
          this.disFlag = true
        }
        this.$set(this.dataForm, 'workDeptId', data.workDeptId || 0)
        this.$set(this.dataForm, 'linkType', data.linkType)
        if (!this.dataForm.workDeptId) {
          this.dataForm.linkName = []
          this.dataForm.linkIds = []
          this.disFlag = false
          this.dataForm.workDeptId = ''
          this.dataForm.linkType = 1
        }
        this.visible = true
        this.$nextTick(() => {
          if (this.dataForm.workDeptId) {
            getObj(this.dataForm.workDeptId, this.dataForm.linkType).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      getWorkDeptList() {
        let deptTypes = []
        deptTypes.push(2)
        deptTypes.push(8)
        let obj = {
          deptTypes: deptTypes
        }
        getDeptInfoByDeptTypes(obj).then(response => {
          if(response.data.code == '0'){
            this.workDeptOption = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.type == 'edit') {
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            } else {
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            }
          }
        })
      },
      changeType() {
        this.dataForm.linkName = []
        this.dataForm.linkIds = []
      },
      //  打开查询部门弹窗
      openDeptDialog() {
        if (this.dataForm.linkType == 1) {
          this.deptVisible = true
          this.$nextTick(() => {
            this.$refs.deptDialog.init()
          })
        } else {
          this.unitVisible = true
          this.$nextTick(() => {
            this.$refs.unitDialog.init()
          })
        }
      },
      //  获取选择部门数据
      getDeptData(data) {
        let ids = []
        let names = []
        if (data.length) {
          data.forEach(item => {
            ids.push(item.id)
            names.push(item.name)
          })
        }
        this.dataForm.linkName = names.join(',')
        this.dataForm.linkIds = ids
      },
    }
  }
</script>
